package com.magic.process.operator;

import com.magic.enums.MeetingEventEnum;
import com.magic.enums.OperatorOrderEnum;
import com.magic.handler.MeetingHelper;
import com.magic.process.IMeetingOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class MeetingTaskOperator implements IMeetingOperator {

	protected static final Set<MeetingEventEnum> supportItems = new HashSet<>();

  static {
    supportItems.add(MeetingEventEnum.RESERVE);
    supportItems.add(MeetingEventEnum.SYNC_RESERVE);
    supportItems.add(MeetingEventEnum.UPDATE);
    supportItems.add(MeetingEventEnum.SYNC_UPDATE);
    supportItems.add(MeetingEventEnum.AUDIT);
    supportItems.add(MeetingEventEnum.EXTEND);
    supportItems.add(MeetingEventEnum.CANCEL);
    supportItems.add(MeetingEventEnum.END);
  }

  /**
   * 设置最前面执行
   * @return
   */
  @Override
  public OperatorOrderEnum currentOrder() {
    return OperatorOrderEnum.FIRST;
  }

  @Override public Boolean support(MeetingEventEnum meetingEventEnum) {
    boolean support = supportItems.contains(meetingEventEnum);
    log.debug("会议定时任务执行器支持{}事件: {}", meetingEventEnum, support);
    return support;
  }

  @Override
  public MeetingHelper[] beforeEvent(MeetingEventEnum meetingEventEnum, MeetingHelper... helpers) {
    return helpers;
  }

  @Override
  public MeetingHelper[] afterEvent(MeetingEventEnum meetingEventEnum, MeetingHelper... helpers) {
    log.info("after meeting event = {} operate = {}", meetingEventEnum.name(),
        this.getClass().getName());
    long start = System.currentTimeMillis();
    log.info("会议定时任务执行器操作{},事件: {}, 线程：{}", meetingEventEnum, helpers, Thread.currentThread());

    log.info("事件支持operator:{},的afterEvent执行时间为:{}, 线程名:{}", this, System.currentTimeMillis() - start, Thread.currentThread());
    return helpers;
  }

}

